home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_3 / issue06 / c_part5 / !Sysvars / c / wexample
Encoding:
Text File  |  1990-01-21  |  4.9 KB  |  199 lines

  1. /*
  2.     Sysvars - example window program to display selected system variables in a window.
  3.     Icon-bar menu allows pattern for variable names to be selected.
  4.     Single-click launches variable window if none present.
  5.     *
  6.     Chris Dollin, January 1990
  7.     Code may be freely mangled. No rights reserved. No warranty. 
  8. */
  9.  
  10. #include "wimp.h"
  11. #include "wimpt.h"
  12. #include "win.h"
  13. #include "event.h"
  14. #include "baricon.h"
  15. #include "res.h"
  16. #include "resspr.h"
  17. #include "menu.h"
  18. #include "template.h"
  19. #include "dbox.h"
  20. #include "werr.h"
  21. #include "txt.h"
  22. #include "flex.h"
  23. #include "heap.h"
  24. #include "visdelay.h"
  25.  
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. /*
  31.     Sundry constants
  32. */
  33.  
  34. #define Example_menu_info     1        /* Positions of menu items within bar-icon menu */
  35. #define Example_menu_filter   2        /* (info should be first; quit should be last) */
  36. #define Example_menu_quit     3
  37.  
  38. #define Filter_Variable       0        /* Writeable icon inside selection box */
  39.  
  40. #define Example_info_field    4        /* Position of version field in info box */
  41.  
  42. /*
  43.     Top-level data
  44. */
  45.  
  46. static char *example_Version_String = "1.00 (30th November 1989)";
  47.  
  48. static menu example_menu = (menu) 0;
  49.  
  50. static txt a_text = (txt) NULL;
  51.  
  52. static char Filter_Variable_String[100];
  53.  
  54. /*
  55.     Reading selected system variables and inserting their names & values into
  56.     a txt buffer.
  57. */
  58.  
  59. #define OS_READ_VAR_VAL 0x23
  60.  
  61. static void sysvars_to_text( txt t, char *var_filter )
  62.     {
  63.     char buffer[255];
  64.     os_error *err;
  65.     os_regset regs;
  66.     regs.r[0] = (int) var_filter;
  67.     regs.r[1] = (int) buffer;
  68.     regs.r[3] = (int) 0;
  69.     txt_setdot( t, 0 );
  70.     txt_delete( t, txt_size( t ) );
  71.     visdelay_begin(); 
  72.     do
  73.         {
  74.         regs.r[2] = sizeof( buffer );                  
  75.         regs.r[4] = 3;
  76.         err = os_swix( os_X | OS_READ_VAR_VAL, ®s );
  77.         if (err == NULL && regs.r[2] > 0)
  78.             {
  79.             buffer[regs.r[2]] = '\0';
  80.             txt_insertstring( t, buffer );
  81.             txt_insertstring( t, " = " );
  82.             txt_insertstring( t, (char *) regs.r[3] );
  83.             txt_insertstring( t, "\n" );
  84.             txt_setdot( t, txt_size( t ) );
  85.             }
  86.         }
  87.         while (err == NULL);
  88.     txt_setdot( t, 0 );
  89.     visdelay_end();
  90.     }
  91.  
  92. /*
  93.     Event handler called on a left click on the icon.
  94. */
  95.  
  96. static void example_iconclick( wimp_i icon )
  97.     {
  98.     if (a_text == NULL) txt_show( a_text = txt_new( "<unset>" ) );
  99.     txt_settitle( a_text, Filter_Variable_String );
  100.     sysvars_to_text( a_text, Filter_Variable_String );
  101.     }
  102.  
  103. /*
  104.     Display the program info box - called from the menu processor.
  105. */
  106.  
  107. static void example_info_about_program(void)
  108.     {
  109.     dbox d = dbox_new( "ProgInfo" );  /* Dialogue box handle */
  110.     if (d != NULL)
  111.         {
  112.         dbox_setfield( d, Example_info_field, example_Version_String );
  113.         dbox_show( d );
  114.         dbox_fillin( d );
  115.         dbox_dispose( &d );
  116.         }
  117.     }
  118.  
  119. /*
  120.     Display dialogue box for filter setting
  121. */
  122.  
  123.  
  124. static void example_set_filters(void)
  125.     {
  126.     dbox d = dbox_new( "Variables" );
  127.     if (d)
  128.         {
  129.         dbox_setfield( d, Filter_Variable, Filter_Variable_String );
  130.         dbox_show( d );
  131.         dbox_fillin( d );
  132.         dbox_getfield( d, Filter_Variable, Filter_Variable_String, 100 );
  133.         dbox_dispose( &d );
  134.         }
  135.     }
  136.  
  137. /*
  138.     Event handler for the menu.
  139. */
  140.  
  141. static void example_menuproc(void *handle, char *hit)
  142.     {
  143.     handle = handle; /* We don't need the handle: this stops compiler warning */
  144.  
  145.     switch (hit[0])
  146.         {
  147.         case Example_menu_info:
  148.             example_info_about_program();
  149.             break;
  150.       
  151.         case Example_menu_filter:
  152.             example_set_filters();
  153.             if (a_text) example_iconclick( (wimp_i) 0 );
  154.             break;
  155.  
  156.         case Example_menu_quit:
  157.             exit(0);
  158.             break;
  159.         }
  160.     }
  161.  
  162. /*
  163.     Initialise the program, returning TRUE if it was all OK.
  164. */
  165.  
  166. static BOOL example_initialise(void)
  167.     {
  168.     flex_init();                     /* Flex store management */
  169.     heap_init( TRUE );               /* Heap store ditto */
  170.     wimpt_init("Sysvars browser");   /* Main wimp initialisation */
  171.     res_init("Sysvars");             /* Resources */
  172.     resspr_init();                   /* Application sprites */
  173.     template_init();                 /* Templates */
  174.     dbox_init();                     /* Dialogue boxes */
  175.     visdelay_init();                 /* For the hourglass */
  176.  
  177.     strcpy( Filter_Variable_String, "*" );
  178.  
  179.     if ((example_menu = menu_new("Example", ">Info,>Filter,Quit")) == NULL)
  180.         return FALSE;
  181.  
  182.     baricon( "!Sysvars", 1 /* Wimp sprite area */, example_iconclick );
  183.     if (!event_attachmenu( win_ICONBAR, example_menu, example_menuproc, 0 ))
  184.         return FALSE; 
  185.  
  186.     return TRUE;
  187. }
  188.  
  189. /*
  190.     At last, here's -main-.
  191. */
  192.  
  193. int main( int argc, char *argv[] )
  194.     {
  195.     if (example_initialise())
  196.         while (TRUE) event_process();
  197.     return 0;
  198.     }
  199.